﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion
using FI.Core.Properties;
using FI.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace FI.Core
{
    /// <summary>
    /// A <see cref="Exception"/> in whcih an <see cref="ErrorInfo"/> is wrapped.
    /// </summary>
    /// <summary>
    /// Generic exception
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2240:ImplementISerializableCorrectly")]
    [Serializable]
    public class ErrorInfoBasedException : Exception
    {
        private string message;

        /// <summary>
        /// Get the <see cref="ErrorInfo"/> in which detailed error information is included.
        /// </summary>
        public ErrorInfo ErrorInfo { get; private set; }

        /// <summary>
        /// Create a new  <see cref="ErrorInfoBasedException"/>.
        /// </summary>
        /// <param name="errorCode">The predefined error code.</param>
        /// <param name="message">A friendly error message. The <see cref="ErrorInfo"/>'s message will be used if it is not expl</param>
        public ErrorInfoBasedException(string errorCode, string message = null)
        {
            Guard.ArgumentNotNullOrEmpty(errorCode, "errorCode");
            ErrorInfo errorInfo;
            if (!Errors.TryGetError(errorCode, out errorInfo))
            {
                throw new ArgumentException(Resources.ExceptionInvalidErrorCode.Format(new object[] { errorCode }), "errorCode");
            }
            this.message = message;
            this.ErrorInfo = errorInfo;
        }

        /// <summary>
        /// Create a new  <see cref="ErrorInfoBasedException"/>.
        /// </summary>
        /// <param name="errorInfo"> The <see cref="ErrorInfo"/> in which detailed error information is included.</param>
        /// <param name="message">A friendly error message. The <see cref="ErrorInfo"/>'s message will be used if it is not explicitly specified.</param>
        public ErrorInfoBasedException(ErrorInfo errorInfo, string message = null)
        {
            Guard.ArgumentNotNull(errorInfo, "errorInfo");
            this.message = message;
            this.ErrorInfo = errorInfo;
        }

        /// <summary>
        /// Create a new  <see cref="ErrorInfoBasedException"/>.
        /// </summary>
        public ErrorInfoBasedException(string errorCode, Exception innerException)
            : base("", innerException)
        {
            Guard.ArgumentNotNullOrEmpty(errorCode, "errorCode");
            ErrorInfo errorInfo;
            if (!Errors.TryGetError(errorCode, out errorInfo))
            {
                throw new ArgumentException(Resources.ExceptionInvalidErrorCode.Format(new object[] { errorCode }), "errorCode");
            }
            this.ErrorInfo = errorInfo;
        }

        /// <summary>
        /// Gets the error message.
        /// </summary>
        public override string Message
        {
            get
            {
                return string.IsNullOrEmpty(message) ? this.ErrorInfo.Message : message;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorInfoBasedException"/> class.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// The <paramref name="info"/> parameter is null.
        /// </exception>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">
        /// The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0).
        /// </exception>
        protected ErrorInfoBasedException(
          SerializationInfo info,
          StreamingContext context)
            : base(info, context) { }
    }
}
